﻿using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using eShop.DataAccess.Entity;

namespace eShop.DataAccess
{
    public partial class UserProfileDAL : BaseDAL
    {
        public static bool Insert(UserProfile userProfile)
        {
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("UserProfile_InsertOrUpdate");
            db.AddOutParameter(dbCommand, "Id", DbType.Int32, 4);
            db.AddInParameter(dbCommand, "UserId", DbType.Int32, userProfile.UserId);
            db.AddInParameter(dbCommand, "FirstName", DbType.String, userProfile.FirstName);
            db.AddInParameter(dbCommand, "LastName", DbType.String, userProfile.LastName);
            db.AddInParameter(dbCommand, "EmailAddress", DbType.String, userProfile.EmailAddress);
            if (userProfile.DateOfBirth.HasValue)
                db.AddInParameter(dbCommand, "DateOfBirth", DbType.DateTime, userProfile.DateOfBirth);
            else
                db.AddInParameter(dbCommand, "DateOfBirth", DbType.DateTime, DBNull.Value);
            db.AddInParameter(dbCommand, "Gender", DbType.String, userProfile.Gender);
            db.AddInParameter(dbCommand, "PhoneNumber", DbType.String, userProfile.PhoneNumber);

            if (db.ExecuteNonQuery(dbCommand) > 0)
            {
                userProfile.Id = Convert.ToInt32(db.GetParameterValue(dbCommand, "@Id"));
                return true;
            }
            return false;
        }

        public static bool Update(UserProfile userProfile)
        {
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("UserProfile_InsertOrUpdate");
            db.AddInParameter(dbCommand, "Id", DbType.Int32, userProfile.Id);
            db.AddInParameter(dbCommand, "UserId", DbType.Int32, userProfile.UserId);
            db.AddInParameter(dbCommand, "FirstName", DbType.String, userProfile.FirstName);
            db.AddInParameter(dbCommand, "LastName", DbType.String, userProfile.LastName);
            db.AddInParameter(dbCommand, "EmailAddress", DbType.String, userProfile.EmailAddress);
            if (userProfile.DateOfBirth.HasValue)
                db.AddInParameter(dbCommand, "DateOfBirth", DbType.DateTime, userProfile.DateOfBirth);
            else
                db.AddInParameter(dbCommand, "DateOfBirth", DbType.DateTime, DBNull.Value);
            db.AddInParameter(dbCommand, "Gender", DbType.String, userProfile.Gender);
            db.AddInParameter(dbCommand, "PhoneNumber", DbType.String, userProfile.PhoneNumber);

            if (db.ExecuteNonQuery(dbCommand) > 0)
                return true;
            return false;
        }

        public static List<UserProfile> GetAll(Boolean allowPaging = false, Int32 pageIndex = 0, Int32 pageSize = 0, String orderColumn = "Id", Boolean isAscending = false)
        {
            List<UserProfile> userProfileList = new List<UserProfile>();
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("UserProfile_GetAll");
            db.AddInParameter(dbCommand, "AllowPaging", DbType.Boolean, allowPaging);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, pageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, pageSize);
            db.AddInParameter(dbCommand, "OrderColumn", DbType.String, orderColumn);
            db.AddInParameter(dbCommand, "IsAscending", DbType.Boolean, isAscending);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                userProfileList = GetEntitiesFromReader(dataReader);
            }
            return userProfileList;
        }

        public static List<UserProfile> GetByKeys(Int32? id, Int32? userId)
        {
            List<UserProfile> userProfileList = new List<UserProfile>();
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("UserProfile_GetByKeys");
            if (id.HasValue)
                db.AddInParameter(dbCommand, "Id", DbType.Int32, id);
            if (userId.HasValue)
                db.AddInParameter(dbCommand, "UserId", DbType.Int32, userId);

            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                userProfileList = GetEntitiesFromReader(dataReader);
            }
            return userProfileList;
        }

        public static bool DeleteByKeys(Int32? id, Int32? userId)
        {
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("UserProfile_DeleteByKeys");
            if (id.HasValue)
                db.AddInParameter(dbCommand, "Id", DbType.Int32, id);
            if (userId.HasValue)
                db.AddInParameter(dbCommand, "UserId", DbType.Int32, userId);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return true;
            return false;
        }

        private static List<UserProfile> GetEntitiesFromReader(IDataReader dataReader)
        {
            List<UserProfile> userProfileList = new List<UserProfile>();
            while (dataReader.Read())
            {
                userProfileList.Add(GetEntityFromReader(dataReader));
            }
            return userProfileList;
        }

        private static UserProfile GetEntityFromReader(IDataReader dataReader)
        {
            UserProfile userProfile = new UserProfile();
            userProfile.Id = (Int32)dataReader["Id"];
            userProfile.UserId = (Int32)dataReader["UserId"];
            userProfile.FirstName = (String)dataReader["FirstName"];
            userProfile.LastName = (String)dataReader["LastName"];
            userProfile.EmailAddress = (String)dataReader["EmailAddress"];
            if (dataReader["DateOfBirth"] == System.DBNull.Value)
                userProfile.DateOfBirth = null;
            else
                userProfile.DateOfBirth = (DateTime)dataReader["DateOfBirth"];
            userProfile.Gender = (String)dataReader["Gender"];
            userProfile.PhoneNumber = (String)dataReader["PhoneNumber"];
            return userProfile;
        }
    }
}

